package com.datamining.util;

import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static java.lang.Math.log;

public class AlgorithmUtil {

	/**
	 * 信息熵公式
	 * @param list 各节点的值
	 * @return 信息熵
	 */
	public double entropy (List<Integer> list) {
		Double data = 0.000000;

		for (int i = 0; i < list.size(); i++) {
		    if (list.get(i) == 0) {
		        return format(0.000);
            }
			data += list.get(i);
		}

		Double result = new Double(0.000000);

		for (int i = 0; i < list.size(); i++) {
			Double p = Double.valueOf(list.get(i)/data);
			result += -(p*(log((double)p)/log((double)2)));
		}

		result = format(result);
		return result;
	}

	public double info(List<Double> list) {

	    return 0;
    }

	/**
	 *
	 * @param math 需要保留3位小数的值
	 * @return 保留3位小数的double
	 */
	public double format(Double math) {
		DecimalFormat df = new DecimalFormat("0.000");
		return Double.parseDouble(df.format(math));
	}

	/**
	 * 将list中指定的元素移除
	 * @param dataList 需要移除元素的list
	 * @param property 需要移除的元素
	 * @return 移除后的list
	 */
	public List<String> remove(List<String> dataList , String property) {
		Iterator<String> it = dataList.iterator();
		while(it.hasNext()){
			String x = it.next();
			if(x.equals(property)){
				it.remove();
			}
		}
		return dataList;
	}

	public String findMax(Map<String,Double> map) {
	    Double value = 0.0;
	    String keyMark = null;
        Iterator<String> iter = map.keySet().iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            if (value < map.get(key)) {
                value = map.get(key);
                keyMark = key;
            }
        }
	    return keyMark;
    }

}
